package com.dongliushui.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dongliushui.bean.TextMsg;
import com.dongliushui.constants.MsgTypes;
import com.dongliushui.responsemsg.Article;
import com.dongliushui.responsemsg.ResponseArticleMsg;
import com.dongliushui.responsemsg.ResponseTextMsg;
import com.dongliushui.thirdpartapi.BeautyApiCaller;
import com.dongliushui.thirdpartapi.TuringApiCaller;
import com.dongliushui.thirdpartapi.WeatherApiCaller;
import com.dongliushui.util.VelocityHelper;

/**
 * @ClassName: TextMsgProcessor
 * @Description: 文本消息业务处理类
 * @author zhutulang
 * @date 2016年1月12日
 * @version V1.0
 */
@Component
public class TextMsgProcessor {

	@Autowired
	private WeatherApiCaller WeatherApiCaller;
	@Autowired
	private BeautyApiCaller beautyApiCaller;
	@Autowired
	private TuringApiCaller turingApiCallerj;
	
	private TextMsgProcessor(){
		
	}
	
	/**
	 * <p>Title: process<／p>
	 * <p>Description:处理文本消息 <／p>
	 * @param textMsg
	 * @return
	 * @author zhutulang
	 * @version 1.0
	 * @throws Exception 
	 */
	public String process(TextMsg textMsg) throws Exception{
		String toUserName = textMsg.getFromUserName();
		String fromUserName = textMsg.getToUserName();
		String getMsg = textMsg.getContent();
		String msgId = textMsg.getMsgId();
    	String content = null;
    	String createTime = String.valueOf(System.currentTimeMillis());
    	if("1".equals(getMsg) || "0".equals(getMsg)){
    		content = WeatherApiCaller.call("深圳");
    		ResponseTextMsg responseMsg = 
        			new ResponseTextMsg(toUserName, fromUserName, 
        					createTime, MsgTypes.TEXT, msgId, content);
            //将responseMsg 转换成xml字符串返回
    	    return  VelocityHelper.getXmlFromResponseMsg("ResponseTextMsg.vm",  responseMsg);
    	}else if("2".equals(getMsg)){
    		List<Article> articles = beautyApiCaller.call(5);
    		ResponseArticleMsg responseArticleMsg =
    				new ResponseArticleMsg(toUserName, fromUserName, createTime, MsgTypes.NEWS, articles);
    		return  VelocityHelper.getXmlFromResponseMsg("ResponseArticleMsg.vm",  responseArticleMsg);
    	}else if("help".equals(getMsg)){
    		content = "you发给me的是文本消息："+getMsg
    				+"\r\n发送0或1查询深圳实时天气，发送2查看美女图片";
    		ResponseTextMsg responseMsg = 
        			new ResponseTextMsg(toUserName, fromUserName, 
        					createTime, MsgTypes.TEXT, msgId, content);
            //将responseMsg 转换成xml字符串返回
    	    return  VelocityHelper.getXmlFromResponseMsg("ResponseTextMsg.vm",  responseMsg);
    	}else {
    		content = turingApiCallerj.call(getMsg);
    		ResponseTextMsg responseMsg = 
        			new ResponseTextMsg(toUserName, fromUserName, 
        					createTime, MsgTypes.TEXT, msgId, content);
    	    return  VelocityHelper.getXmlFromResponseMsg("ResponseTextMsg.vm",  responseMsg);
    	}
	}
}
